package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfChannelInfo;
import com.ocom.common.entity.master.EntityPayChannel;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.GzhEnum;
import com.ocom.common.request.payconfig.AddConfigureInfoRequest;
import com.ocom.common.request.payconfig.DeleteConfigureInfoRequest;
import com.ocom.common.request.payconfig.GetConfigureInfoRequest;
import com.ocom.common.request.payconfig.UpdateConfigureInfoRequest;
import com.ocom.common.request.web.OneConfigureInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.GzhPayVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoChannelInfo;
import com.ocom.payconfig.service.PayChannelService;
import com.ocom.payconfig.service.XfChannelInfoService;
import com.ocom.payconfig.service.XfMerchantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class XfChannelInfoServiceImpl extends ServiceImpl<IDaoChannelInfo, EntityXfChannelInfo> implements XfChannelInfoService {

	@Autowired
	PayChannelService payChannelService;

	@Autowired
	XfMerchantInfoService xfMerchantInfoService;

	@Autowired
	IDaoChannelInfo daoChannelInfo;

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public List<EntityXfChannelInfo> getConfigureList(Long companyId, GetConfigureInfoRequest request) {
		return daoChannelInfo.getChannelList(companyId,request.getMerchId(),request.getId());
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfChannelInfo OneConfigureInfo(Long companyId, OneConfigureInfoRequest request) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", request.getId());
		return this.getOne(queryWrapper);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfChannelInfo addConfigureInfo(Long companyId, AddConfigureInfoRequest request) {
		//TODO 判断机器号重复
		EntityXfChannelInfo entityXfChannelInfo = new EntityXfChannelInfo();
		entityXfChannelInfo.setComId(request.getComId().longValue());
		//payWay 要存code
		entityXfChannelInfo.setPayWay(request.getPayWay());
		entityXfChannelInfo.setPayType(request.getPayType());
		entityXfChannelInfo.setMerchId(request.getMerchId());
		entityXfChannelInfo.setDeviceNo(request.getDeviceNo());
		entityXfChannelInfo.setChannelParam(request.getChannelParam());
		entityXfChannelInfo.setLink(request.getLink());
		entityXfChannelInfo.setPayGzh(request.getPayGzh());
		entityXfChannelInfo.setCreateUser(request.getCreateUser());
		entityXfChannelInfo.setCreateTime(new Date());
		this.save(entityXfChannelInfo);
		return entityXfChannelInfo;
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfChannelInfo updateConfigureInfo(Long companyId, UpdateConfigureInfoRequest request) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("merch_id", request.getMerchId());
		queryWrapper.eq("com_id", request.getComId());
		queryWrapper.last("limit 1");
		EntityXfChannelInfo entityXfChannelInfo = this.getOne(queryWrapper);
		if (entityXfChannelInfo == null) {
			return null;
		}
		if (request.getPayWay() > 0) {
			//payWay 要存code
			entityXfChannelInfo.setPayWay(request.getPayWay());
		}
		if (request.getMerchId() > 0) {
			entityXfChannelInfo.setMerchId(request.getMerchId());
		}
		if (!CommonUtil.isNull(request.getChannelParam())) {
			entityXfChannelInfo.setChannelParam(request.getChannelParam());
		}
		if (!CommonUtil.isNull(request.getPayType())) {
			entityXfChannelInfo.setPayType(request.getPayType());
		}
		if (!CommonUtil.isNull(request.getDeviceNo())) {
			entityXfChannelInfo.setDeviceNo(request.getDeviceNo());
		}
		if (!CommonUtil.isNull(request.getUpdateUser())) {
			entityXfChannelInfo.setUpdateUser(request.getUpdateUser());
		}
		if (!CommonUtil.isNull(request.getPayGzh())) {
			entityXfChannelInfo.setPayGzh(request.getPayGzh());
		}
		if (!CommonUtil.isNull(request.getPayDc())) {
			entityXfChannelInfo.setPayDc(request.getPayDc());
		}
		entityXfChannelInfo.setUpdateTime(new Date());
		boolean b = this.updateById(entityXfChannelInfo);
		if (!b) {
			return null;
		}
		return entityXfChannelInfo;
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public boolean deleteConfigureInfo(Long companyId, DeleteConfigureInfoRequest request) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", request.getComId());
		queryWrapper.eq("id", request.getId());
		return this.remove(queryWrapper);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public Result getDeleteConfigureInfo(Long companyId, Integer id) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", companyId);
		queryWrapper.eq("id", id);
		EntityXfChannelInfo entityXfChannelInfo = this.getOne(queryWrapper);
		this.remove(queryWrapper);
		Map<String, Object> map = new HashMap<>();
		map.put("payWay", entityXfChannelInfo.getPayWay());
		map.put("companyId", companyId);
		return ResultUtil.success(map);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public List<EntityXfChannelInfo> getSignChannelList(Long companyId, Integer payWay) {
		return daoChannelInfo.getSignList(companyId, payWay);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public List<GzhPayVo> getGzhPayList(Long companyId, Long merchId,List<EntityPayChannel> payChannels) {
		Map<Integer, String> stringMap = payChannels.stream()
				.collect(Collectors.toMap(EntityPayChannel::getPayWay, EntityPayChannel::getName));

		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper();
		queryWrapper.eq("com_id", companyId);
		if (null != merchId && 0 < merchId) {
			queryWrapper.eq("merch_id", merchId);
		}else{
			queryWrapper.eq("merch_id", 0);
		}
		queryWrapper.ne("pay_gzh", "000000");
		queryWrapper.ne("pay_gzh", "0000000");
		List<EntityXfChannelInfo> list = this.list(queryWrapper);




		List<GzhPayVo>  list1 =new ArrayList<>();

		for(EntityXfChannelInfo entityXfChannelInfo : list){
			char[] chars = entityXfChannelInfo.getPayGzh().toCharArray();

			List<Integer> types = list1.stream().map(GzhPayVo::getType).collect(Collectors.toList());


			for (int i = 0; i < chars.length; i++) {

				if(new Character(chars[i]).toString().equals("1") && !types.contains(1)){
					GzhPayVo gzhPayVo =new GzhPayVo();
					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
					}
					gzhPayVo.setType(i+1);
					gzhPayVo.setName(GzhEnum.getShowValueByKey(i+1));
					list1.add(gzhPayVo);
				}

			}


//
//			//判断第一个0
//			if(chars.length>=1){
//				if(new Character(chars[0]).toString().equals("1") && !types.contains(1)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(1);
//					gzhPayVo.setName(GzhEnum.getValueByKey(1));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=2){
//				if(new Character(chars[1]).toString().equals("1") && !types.contains(2)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(2);
//					gzhPayVo.setName(GzhEnum.getValueByKey(2));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=3){
//				if(new Character(chars[2]).toString().equals("1") && !types.contains(3)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(3);
//					gzhPayVo.setName(GzhEnum.getValueByKey(1));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=4){
//				if(new Character(chars[3]).toString().equals("1") && !types.contains(4)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(4);
//					gzhPayVo.setName(GzhEnum.getValueByKey(4));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=5){
//				if(new Character(chars[4]).toString().equals("1") && !types.contains(5)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(5);
//					gzhPayVo.setName(GzhEnum.getValueByKey(1));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=6){
//				if(new Character(chars[5]).toString().equals("1") && !types.contains(6)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(6);
//					gzhPayVo.setName(GzhEnum.getValueByKey(6));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=7){
//				if(new Character(chars[6]).toString().equals("1") && !types.contains(7)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(7);
//					gzhPayVo.setName(GzhEnum.getValueByKey(7));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=8){
//				if(new Character(chars[7]).toString().equals("1") && !types.contains(8)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(8);
//					gzhPayVo.setName(GzhEnum.getValueByKey(8));
//					list1.add(gzhPayVo);
//				}
//			}
//			if(chars.length>=9){
//				if(new Character(chars[8]).toString().equals("1") && !types.contains(9)){
//					GzhPayVo gzhPayVo =new GzhPayVo();
//					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
//					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
//						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
//					}
//					gzhPayVo.setType(9);
//					gzhPayVo.setName(GzhEnum.getValueByKey(9));
//					list1.add(gzhPayVo);
//				}
//			}
		}
		return list1;
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public List<GzhPayVo> getGzhPayList2(Long companyId, Long merchId,List<EntityPayChannel> payChannels) {
		Map<Integer, String> stringMap = payChannels.stream()
				.collect(Collectors.toMap(EntityPayChannel::getPayWay, EntityPayChannel::getName));

		QueryWrapper<EntityXfChannelInfo> queryWrapper = new QueryWrapper();
		queryWrapper.eq("com_id", companyId);
		if (null != merchId && 0 < merchId) {
			queryWrapper.eq("merch_id", merchId);
		}else{
			queryWrapper.eq("merch_id", 0);
		}
		queryWrapper.ne("pay_dc", "000000");
		queryWrapper.ne("pay_dc", "0000000");
		List<EntityXfChannelInfo> list = this.list(queryWrapper);


		List<GzhPayVo>  list1 =new ArrayList<>();

		for(EntityXfChannelInfo entityXfChannelInfo : list){
			char[] chars = entityXfChannelInfo.getPayDc().toCharArray();

			List<Integer> types = list1.stream().map(GzhPayVo::getType).collect(Collectors.toList());


			for (int i = 0; i < chars.length; i++) {

				if(new Character(chars[i]).toString().equals("1") && !types.contains(1)){
					GzhPayVo gzhPayVo =new GzhPayVo();
					gzhPayVo.setPayWay(entityXfChannelInfo.getPayWay());
					if(stringMap.containsKey(entityXfChannelInfo.getPayWay())){
						gzhPayVo.setPayWayName(stringMap.get(entityXfChannelInfo.getPayWay()));
					}
					gzhPayVo.setType(i+1);
					gzhPayVo.setName(GzhEnum.getShowValueByKey(i+1));
					list1.add(gzhPayVo);
				}

			}

		}
		return list1;
	}


}
